package collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayToListDemo {
    public static void main(String[] args) {
        String[] array={"one","two","three","four","five"};
        System.out.println("array:"+ Arrays.toString(array));

        //asList()方法会返回内部的ArrayList，内部直接引用给定数组array
        List<String> list=Arrays.asList(array);
        System.out.println("list:"+list);

        list.set(1,"six");//数组跟集合有对应关系，不是单纯的分开
        System.out.println("array:"+ Arrays.toString(array));
        System.out.println("list:"+list);

        //对数组操作后，集合也会相应的改变
        array[2]="seven";//对集合的操作就是对底层数组的的修改
        System.out.println("array:"+ Arrays.toString(array));
        System.out.println("list:"+list);


        //数组是定长的，不支持增删，除非用新数组复制
//        list.add("!!!!");//集合的增删会让底层数组产生异常报错UnsupportedOperationException
//        System.out.println("array:"+ Arrays.toString(array));
//        System.out.println("list:"+list);

        List<String> list2=new ArrayList<>();
        list2.addAll(list);//可简化
        List<String> list3=new ArrayList<>(list);//把list直接传进list3
        System.out.println("list3:"+list3);
        System.out.println("list2:"+list2);
        list2.add("!!!!");
        System.out.println("list2:"+list2);

    }

}
